<?php
namespace app\common\model;
use think\Db;
class DogFamily extends \think\Model {

    protected $table = 'up_dog_family';

    public static $udf_sex = array(
        '1'=>'公',
        '2'=>'母',
        '3'=>'不详'
    );
    public static $udf_condition = array(
        '0'=>'待救助',
        '1'=>'已救助',
        '2'=>'家养宠物',
        '3'=>'不详',
    );
    public static $udf_size = array(
        '1'=>'小型犬',
        '2'=>'中型',
        '3'=>'大型犬',
        '4'=>'不详'
    );
    public static $udf_age = array(
        '1'=>'不满1岁',
        '2'=>'1-3岁',
        '3'=>'4-6岁',
        '4'=>'7岁以上',
        '5'=>'不详',
    );
    //狗是否注射狂犬疫苗
    public static $udf_dog_vaccine = array(
        '1'=>'否',
        '2'=>'是',
        '3'=>'不详',

    );
    //狗是否体外驱虫
    public static $udf_dog_outside = array(
        '1'=>'否',
        '2'=>'是',
        '3'=>'不详',
    );
    //狗是否体内驱虫
    public static $udf_dog_inside = array(
        '1'=>'否',
        '2'=>'是',
        '3'=>'不详',
    );
    //狗是否绝育
    public static $udf_dog_sterilization = array(
    '1'=>'否',
    '2'=>'是',
    '3'=>'不详',
   );
    //狗是否残疾
    public static $udf_dog_disability = array(
    '1'=>'否',
    '2'=>'是',
    '3'=>'不详',
    );
    public function relData($pageSize,$search){
        $model = self::instance();
        $where = '';
        if($search[0]==-2){
            $where['a.udf_size'] = ['>',-2];
        }elseif ($search[0]>=-1){
            $where['a.udf_size'] = ['=',$search[0]];
        }
        if($search[1]==-2){
            $where['a.udf_age'] = ['>',-2];
        }elseif ($search[1]>-2){
            $where['a.udf_age'] = ['=',$search[1]];
        }
        if($search[2]==-2){
            $where['a.udf_sex'] =['>',-2];
        }elseif ($search[2]>-2){
            $where['a.udf_sex'] = ['=',$search[2]];
        }
        if($search[3]==-2){
            $where['a.udf_condition'] =['>',-2];
        }elseif ($search[3]>-2){
            $where['a.udf_condition'] = ['=',$search[3]];
        }
        if($search[4]!=''){
            $where['a.udf_name']=['like',"%$search[4]%"];
        }
        if($search[5]==-2){
            $where['a.udf_province'] =['>',$search[5]];
        }elseif ($search[5]>-2){
            $where['a.udf_province'] =['=',$search[5]];
        }
        if($search[6]==-2){
            $where['a.udf_city'] =['>',$search[6]];
        }elseif ($search[6]>-2){
            $where['a.udf_city'] =['=',$search[6]];
        }
        if($search[7]==-2){
            $where['a.udf_district'] =['>',$search[7]];
        }elseif ($search[7]>-2){
            $where['a.udf_district'] =['=',$search[7]];
        }
        $list = $model->alias('a')
                      ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                      ->join('up_area ua1','ua1.district_id = a.udf_province','left')
                      ->join('up_area ua2','ua2.district_id = a.udf_city','left')
                      ->join('up_area ua3','ua3.district_id = a.udf_district','left')
                      ->where($where)
                      ->field('a.*,uuu.user_username,ua1.district as province,ua2.district as city,ua3.district as district')
                      ->order('a.udf_datetime desc')
                      ->paginate($pageSize);
        foreach ($list as $key=>&$vo){
                $whereMap['uco_id'] = array('IN',$vo['udf_character']);
                $udskca_name = Db::table('up_dog_character')->where($whereMap)->column('uco_character');
                if(count($udskca_name)>0){
                    if($vo['udf_character_other']!=''){
                        $vo['udskca_name'] = implode(',',$udskca_name);
                        $vo['udskca_name'] = $vo['udskca_name'].','.$vo['udf_character_other'];
                    }else{
                        $vo['udskca_name'] = implode(',',$udskca_name);
                    }
                }else{
                    $vo['udskca_name'] = $vo['udf_character_other'];
                }
        }
        return $list;
    }

    public function detail($where){
        $model = self::instance();
        $list = $model->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->join('up_area ua1','ua1.district_id = a.udf_province','left')
            ->join('up_area ua2','ua2.district_id = a.udf_city','left')
            ->join('up_area ua3','ua3.district_id = a.udf_district','left')
            ->where($where)
            ->field('a.*,uuu.equipment,uuu.user_attestation,uuu.user_gender,uuu.user_card,uuu.user_username,ua1.district as province,ua2.district as city,ua3.district as district')
            ->find();
            $whereMap['uco_id'] = array('IN',$list['udf_character']);
            $udskca_name = Db::table('up_dog_character')->where($whereMap)->column('uco_character');
            if(count($udskca_name)>0){
                if($list['udf_character_other']!=''){
                    $list['udskca_name'] = implode(',',$udskca_name);
                    $list['udskca_name'] = $list['udskca_name'].','.$list['udf_character_other'];
                }else{
                    $list['udskca_name'] = implode(',',$udskca_name);
                }
            }else{
                $list['udskca_name'] = $list['udf_character_other'];
            }
            return $list;
    }
    /**
     * @param $where
     * @return int|string
     * 统计狗窝数量
     */
    public function countNum($where){
        $model = self::instance();
        $count = $model->where($where)->count();
        return $count;
    }

    /**
     * @param $where
     * @return int|string
     * 统计狗窝数量
     */
    public function countDogNum($where){
        $model = self::instance();
        $count = $model->where($where)->count();
        return $count;
    }
    /**
     * @param $search
     * @return false|\PDOStatement|string|\think\Collection
     * excel导出数据
     */
    public function excelData($search){
            $sear =  $search['search'];
            $model = self::instance();
            $where = '';
            if($sear[0]==-2){
                $where['a.udf_size'] = ['>',-2];
            }elseif ($sear[0]>=-1){
                $where['a.udf_size'] = ['=',$sear[0]];
            }
            if($sear[1]==-2){
                $where['a.udf_age'] = ['>',-2];
            }elseif ($sear[1]>-2){
                $where['a.udf_age'] = ['=',$sear[1]];
            }
            if($sear[2]==-2){
                $where['a.udf_sex'] =['>',-2];
            }elseif ($sear[2]>-2){
                $where['a.udf_sex'] = ['=',$sear[2]];
            }
            if($sear[3]==-2){
                $where['a.udf_condition'] =['>',-2];
            }elseif ($sear[3]>-2){
                $where['a.udf_condition'] = ['=',$sear[3]];
            }
            if($sear[4]!=''){
                $where['a.udf_name']=['like',"%$sear[4]%"];
            }
            if($sear[5]==-2){
                $where['a.udf_province'] =['>',$sear[5]];
            }elseif ($sear[5]>-2){
                $where['a.udf_province'] =['=',$sear[5]];
            }
            if($sear[6]==-2){
                $where['a.udf_city'] =['>',$sear[6]];
            }elseif ($sear[6]>-2){
                $where['a.udf_city'] =['=',$sear[6]];
            }
            if($sear[7]==-2){
                $where['a.udf_district'] =['>',$sear[7]];
            }elseif ($sear[7]>-2){
                $where['a.udf_district'] =['=',$sear[7]];
            }
            $list = $model->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                ->join('up_area ua1','ua1.district_id = a.udf_province','left')
                ->join('up_area ua2','ua2.district_id = a.udf_city','left')
                ->join('up_area ua3','ua3.district_id = a.udf_district','left')
                ->where($where)
                ->field('a.*,uuu.equipment,uuu.user_card,uuu.user_gender,uuu.user_attestation,uuu.user_id,uuu.user_username,ua1.district as province,ua2.district as city,ua3.district as district')
                ->select();
            foreach ($list as $key=>&$vo){
                $whereMap['uco_id'] = array('IN',$vo['udf_character']);
                $udskca_name = Db::table('up_dog_character')->where($whereMap)->column('uco_character');
                if(count($udskca_name)>0){
                    if($vo['udf_character_other']!=''){
                        $vo['udskca_name'] = implode(',',$udskca_name);
                        $vo['udskca_name'] = $vo['udskca_name'].','.$vo['udf_character_other'];
                    }else{
                        $vo['udskca_name'] = implode(',',$udskca_name);
                    }
                }else{
                    $vo['udskca_name'] = $vo['udf_character_other'];
                }
            }
            return $list;
    }

    public function echarsTotal($search){
        $model = self::instance();
        $where = '';
        if(isset($search)){
            if($search[0]!=''){
                $where['u.udf_province'] = array('=',$search[0]);
            }
            if($search[1]!=''){
                $where['u.udf_city'] = array('=',$search[1]);
            }
            if($search[2]!=''){
                $where['u.udf_district'] = array('=',$search[2]);
            }
            if($search[3] > 0 && $search[4] > 0){
                $where['u.udf_time'] = array('between',array($search[3],$search[4]));
            }else if($search[3] > 0){
                $where['u.udf_time'] = array('EGT',$search[3]);
            }else if($search[4]> 0){
                $where['u.udf_time'] = array('elt',$search[4]);
            }
        }

        $total = $model->alias('u')
                       ->join('up_area ua1','ua1.district_id = u.udf_province','left')
                       ->join('up_area ua2','ua2.district_id = u.udf_city','left')
                       ->join('up_area ua3','ua3.district_id = u.udf_district','left')
                       ->where($where)
                       ->count();//总的数据
        return $total;
    }

    /**
     * @param $search
     * @param $whereS
     * @return int|string
     * 查询每个的数据
     */
    public function branch($search,$whereS,$type){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['u.udf_province'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.udf_city'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.udf_district'] = array('=',$search[2]);
        }
        if($search[3]==''&&$search[4]==''){
            $time = date('Y-m-d');
            $start = date('Y-m-d',strtotime("-1 month"));
            $where['u.udf_time'] = array('between',array($start,$time));
        } elseif($search[3] > 0 && $search[4] > 0){
            $where['u.udf_time'] = array('between',array($search[3],$search[4]));
        }else if($search[3] > 0){
            $where['u.udf_time'] = array('egt',$search[3]);
        }else if($search[4]> 0){
            $where['u.udf_time'] = array('elt',$search[4]);
        }else{
            $where['u.udf_time'] = array('egt',0);
        }
        $where['u.'.$type] = array('neq',' ');
        $branch = $model->alias('u')
            ->group('u.'.$type)
            ->join('up_area ua1','ua1.district_id = u.udf_province','left')
            ->join('up_area ua2','ua2.district_id = u.udf_city','left')
            ->join('up_area ua3','ua3.district_id = u.udf_district','left')
            ->where($where)
            ->where($whereS)
            ->field('count(udf_id) as value,'.$type)->select();
        //总的数据
        return $branch;
    }
    /**
     * @param $search
     * @param $whereS
     * @return int|string
     * 查询狗窝柱状图数据
     */
    public function columnDog($search,$whereMap,$type){
        $where = '';
        $model = self::instance();
        if($search[0]!=''){
            $where['u.udf_province'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.udf_city'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.udf_district'] = array('=',$search[2]);
        }
        if($type!=''){
            $where['u.udf_condition'] = array('=',$type);
        }
        $branch = $model->alias('u')
            ->where($where)
            ->where($whereMap)
            ->count();
        //总的数据
        return $branch;
    }
    private static $instance;

    public static function instance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }
        return self::$instance;
    }
}